package com.spzx.product.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.core.web.page.TableSupport;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/productSpec")
@Tag(name="商品规格")
public class ProductSpecController extends BaseController {

    @Resource
    ProductSpecService productSpecService;



    @Operation(summary = "查询商品规格列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpec productSpec){
        Page<ProductSpec> page = TableSupport.getPage(ProductSpec.class);

       Page<ProductSpec> list =  productSpecService.listProductSpec(page,productSpec);

        return getDataTable(list);
    }




    @Operation(summary = "获取商品规格详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id")Long id){
         return  success(productSpecService.getInfo(id));
    }




    @Operation(summary = "根据三级分类id 查询分类下的所有规格值列表")
    @GetMapping("/list/{c3id}")
    public AjaxResult listByC3id(@PathVariable("c3id")Long c3id){
        return success(productSpecService.list(Wrappers.lambdaQuery(ProductSpec.class)
                .eq(ProductSpec::getCategoryId , c3id)));
    }

    @Operation(summary = "新增商品规格")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated ProductSpec productSpec){
        productSpec.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.save(productSpec));
    }

    @Operation(summary = "修改商品规格")
    @PutMapping
    public AjaxResult update(@Validated @RequestBody ProductSpec productSpec){
        productSpec.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.updateById(productSpec));
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("{ids}")
    public AjaxResult remove(@PathVariable("ids")List<Long> ids){
        return toAjax(productSpecService.removeBatchByIds(ids));
    }




}
